<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class common_dept_manage extends CI_Controller {


	public function __construct() {
		session_start();
		parent::__construct();

	}


	public function index(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		$data['update_dept']	=false;
		if ($this->right_manage->check_right("update_dept")) {
			$data['update_dept']	=true;
		}
		$data['delete_dept']	=false;
		if ($this->right_manage->check_right("delete_dept")) {
			$data['delete_dept']=true;
		}
		$data['add_dept']	=false;
		if ($this->right_manage->check_right("add_dept")) {
			$data['add_dept']	=true;
		}
		$data['query_dept']	=false;
		if ($this->right_manage->check_right("query_dept")) {
			$data['query_dept']	=true;
		}
		
		$this->load->library("common/dept");
		$dept_id				= intval($this->input->get('dept_id'));
		$data['top_depts']	= $this->dept->get_depts(0);

		$data['depts']= null;
		$data['top_dept_id']  =0;
		$data['second_depts']  =null;
		$data['second_dept_id']  =0;
		$data['has_link']=true;

		if ($dept_id==null) {
			$data['top_depts']		= $this->dept->get_depts(0);
			$data['top_dept_id']		=0;
			$data['depts']			=$data['top_depts'];
		}else{
			$parent_id= $this->dept->get_parent_id($dept_id);
			if ($parent_id==0) {

				$data['top_dept_id']	=$dept_id;
				$data['second_depts'] =$this->dept->get_depts($dept_id);
				$data['depts']		=$data['second_depts'];
			}else{
				$data['has_link']=false;
				$data['top_dept_id'] =$parent_id;
				$data['second_dept_id']  =$dept_id;
				$data['second_depts']  =$this->dept->get_depts($parent_id);
				$data['depts']		=$this->dept->get_depts($dept_id);
			}
		}
		$data['dept_id']=$dept_id;
		$this->load->view("common/dept/dept_main",$data);

	}

	public function add_dept(){
		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_dept",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}


		$this->load->library("common/dept");
		$data['top_depts']	= $this->dept->get_depts(0);


		$this->load->view("common/dept/add_dept",$data);

	}

	public function save_dept(){
		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_dept",$_SESSION['user_id'])) {
			$result['result']	=false;
			$result['message']	='你没有权限使用这个功能';
			echo json_encode($result);die();
		}
		$dept['dept_name'] =trim($this->input->get_post("dept_name"));
		$dept['parent_id'] =$this->input->get_post("parent2");
		if ($dept['parent_id']=="") {
			$dept['parent_id']=$this->input->get_post("parent1");
		}
		$dept['dept_status']=1;
		$this->load->library("common/dept");
		$this->load->helper("common");
		$result =$this->dept->save_dept($dept);
		echo JSON($result);
	}

	public function get_depts(){
		$this->load->helper("common");
		$this->load->library("common/dept");
		$dept_id=$this->input->get('dept_id');
		if ($dept_id==null) {
			$dept_id=0;
		}
		$depts=$this->dept->get_depts($dept_id);

		echo JSON($depts);
	}

	public function delete_dept(){
		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_dept",$_SESSION['user_id'])) {
			$this->load->library("common/user_menu");
			$data['user_menu']=$this->user_menu->get_user_menu();
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("common/dept");
		$dept_id	= intval($this->input->get('id'));
		$this->dept->set_delete_dept($dept_id);
		echo "<script>history.go(-1)</script>";
	}

	public function edit_dept(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();
		
		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_dept",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}


		$dept_id =$this->input->get('id');

		$this->load->library("common/dept");
		$data['top_depts']	= $this->dept->get_depts(0);
		$data['top_dept_id']  =0;
		$data['second_depts']  =null;
		$data['second_dept_id']  =0;
		$data['be_last_dept']=false;
		$dept_info= $this->dept->get_dept_info($dept_id);

		//第一级 省
		if ($dept_info['parent_id']==0) {
			$data['top_dept_id']	=0;
		}else{
			//第二级 市
			$parent_p_id =$this->dept->get_parent_id($dept_info['parent_id']);
			if ($parent_p_id==-1) {
				$data['top_dept_id'] 		=$dept_info['parent_id'];
				$data['second_depts']  	=$this->dept->get_depts($dept_info['parent_id']);
				$data['second_dept_id']  	=0;
				//第三级 区
			}else{
				$data['second_depts']  	=$this->dept->get_depts($parent_p_id);
				$data['top_dept_id'] 		=$parent_p_id;
				$data['second_dept_id']  	=$dept_info['parent_id'];
				$data['be_last_dept']		=true;
			}
		}
		$data['dept_info']=$dept_info;
		$this->load->view("common/dept/edit_dept",$data);
	}

	public function update_dept(){
		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_dept")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$dept['dept_name'] =trim($this->input->get_post("dept_name"));
		$dept['parent_id'] =$this->input->get_post("parent2");
		if ($dept['parent_id']=="") {
			$dept['parent_id']=$this->input->get_post("parent1");
		}
		$dept_id =$this->input->get_post('dept_id');
		$this->load->library("common/dept");
		$this->load->helper("common");
		$result =$this->dept->update_dept($dept,$dept_id);
		echo JSON($result);
	}

	public function get_api_depts(){
		$this->load->helper("common");
		$this->load->library("common/dept");
		$dept_id=$this->input->get('dept_id');
		if ($dept_id==null) {
			$dept_id=0;
		}

		$result['ret_code']=0;
		$result['err_code']=0;
		$depts=$this->dept->get_depts($dept_id);
		$result['data']=$depts;

		echo JSON($result);
	}

}